<?php
namespace Admin;

use Illuminate\Support\Facades\View;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Session ;
use Illuminate\Support\Facades\Validator ;
use Illuminate\Support\Facades\Redirect ;

class PostTypeController extends BaseController
{
    public function index()
    {
        $posttype = \PostType::orderBy('created_at', 'desc')->paginate(7);
        return View::make('admin.PostType.index')->with('posttype', $posttype) ;
    }

    public function create()
    {
        return View::make('admin.PostType.create') ;
    }

    public function store()
    {
        // validate
        // read more on validation at http://laravel.com/docs/validation
        $rules = array(
            'pname'       => 'required',
            'keywords'      => 'required',
            'description' => 'required'
        );
        $validator = Validator::make(Input::all(), $rules);
        
        // process the login
        if ($validator->fails()) {
            return Redirect::to('admin/posttype/create')->withErrors($validator);
        } else {
            // store
            $post = new \PostType();
            $post->pname        =   Input::get('pname');
            $post->keywords     =   Input::get('keywords');
            $post->description  =   Input::get('description');
            
            $pid = $post->save(); 
            
            if ($pid) {
                // redirect
                Session::flash('message', ' 日志类型添加OK！');
                return Redirect::to('admin/posttype');
            }else{
                Session::flash('message', '日志类型添加失败！');
                return Redirect::to('admin/posttype/create');
            }
        }
        
    }

    /**
     * Display the specified resource.
     *
     * @param int $id            
     * @return Response
     */
   /*  public function show($id)
    {
        $post = \Post::find($id);
        return View::make('admin.Post.show')->with('post', $post) ;
    } */

    /**
     * Show the form for editing the specified resource.
     *
     * @param int $id            
     * @return Response
     */
    public function edit($id)
    {
        $posttype = \PostType::find($id);
        return View::make('admin.PostType.edit')->with('posttype', $posttype) ;
    }

    /**
     * Update the specified resource in storage.
     *
     * @param int $id            
     * @return Response
     */
    public function update($id)
    {
        
         // validate
		// read more on validation at http://laravel.com/docs/validation
        $rules = array(
            'pname'       => 'required',
            'keywords'      => 'required',
            'description' => 'required'
        );
		$validator = Validator::make(Input::all(), $rules);

		// process the login
		if ($validator->fails()) {
			return Redirect::to('admin/posttype/' . $id . '/edit')->withErrors($validator);
		} else {
			// store
			$post = \PostType::find($id);
			
			$post->pname        =   Input::get('pname');
			$post->keywords     =   Input::get('keywords');
			$post->description  =   Input::get('description');
			
			$post->save();
			
			// redirect
			Session::flash('message', '日志类型更新OK！');
			return Redirect::to('admin/posttype');
		}
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param int $id            
     * @return Response
     */
    public function destroy($id)
    {

		$post = \PostType::find($id);
		$post->delete();

		// redirect
		Session::flash('message', '['.$post->pname.'] - 已删除！');
		return Redirect::to('admin/posttype');
    }
}
